var app = require("lzbjpk_app");
cc.Class({
	extends: cc.Component,

	properties: {},
	SetPosInfo: function (posEndInfo) {
		this.SetFangZhu(posEndInfo.isFangZhu);
		this.SetDaYingJia(posEndInfo.isWin);
		this.SetName(posEndInfo.name);
		// this.SetId(posEndInfo.id);
		this.SetId(posEndInfo.pid);
		this.SetHead(posEndInfo.pid);
		this.SetWin(posEndInfo.winCount);
		this.SetPing(posEndInfo.flatCount);
		this.SetLost(posEndInfo.loseCount);
		this.SetZongFen(posEndInfo.point);
		this.SetSportPoint(posEndInfo.sportsPoint);
	},
	SetFangZhu: function (isFangZhu) {
		this.SetWndProperty("fangzhu", "active", isFangZhu);
	},
	SetDaYingJia: function (isWin) {
		this.SetWndProperty("dayingjia", "active", isWin);
	},
	SetName: function (name) {
		this.SetWndProperty("touxiang/name", "text", name);
	},
	SetId: function (pid) {
		let id = app[app.subGameName + "_ComTool"]().GetPid(pid);
		this.SetWndProperty("touxiang/id", "text", "ID:" + id);
	},
	SetHead: function (pid) {
		let WeChatHeadImage = this.GetWndComponent("touxiang/head", app.subGameName + "_WeChatHeadImage");
		WeChatHeadImage.onLoad();
		WeChatHeadImage.ShowHeroHead(pid);
	},
	SetWin: function (score) {
		this.SetWndProperty("jiesuan/win/score", "text", score);
	},
	SetPing: function (score) {
		this.SetWndProperty("jiesuan/ping/score", "text", score);
	},
	SetLost: function (score) {
		this.SetWndProperty("jiesuan/lost/score", "text", score);
	},
	SetZongFen: function (score) {
		this.SetWndProperty("zongfen/score", "text", score);
	},
	SetSportPoint: function (sportPoint) {
		this.SetWndProperty("zongfen/lb_sportpoint", "active", app[app.subGameName.toUpperCase() + "RoomMgr"]().unionId > 0);
		this.SetWndProperty("zongfen/lb_sportpoint", "text", "比赛分：" + sportPoint);
	},

	//设置控件属性
	SetWndProperty: function (wndPath, property, value) {

		let wndNode = cc.find(wndPath, this.node);
		if (!wndNode) {
			console.error("SetWndProperty(%s,%s,%s) not find wndPath", wndPath, property, value);
			return
		}

		let wndComponent = null;
		if (property == "text") {
			wndComponent = wndNode.getComponent(cc.Label);
			//可能是label
			if (wndComponent) {
			}
			//没有label可能是editbox
			else if (wndNode.getComponent(cc.RichText)) {
				wndComponent = wndNode.getComponent(cc.RichText);
			}
			else {
				wndComponent = wndNode.getComponent(cc.EditBox);
			}

			if (!wndComponent) {
				console.error("SetWndProperty(%s,%s,%s) wnd not find cc.Label cc.EditBox", wndPath, property, value);
				return
			}
			wndComponent.string = value + "";
		}
		else if (property == "position") {
			wndNode.setPosition(value)
		}
		else if (property == "active") {
			wndNode.active = value;
		}
		else if (property == "color") {
			wndNode.color = value
		}
		else if (property == "image") {
			this.SetWndImage(wndPath, value);
		}
		else if (property == "scale") {
			wndNode.scale = value
		}
		else if (property == "opacity") {
			wndNode.opacity = value
		}
		else if (property == "interactable") {
			wndComponent = wndNode.getComponent(cc.Button);
			if (!wndComponent) {
				console.error("SetWndProperty(%s,%s,%s) wnd not find cc.Button", wndPath, property, value);
				return
			}
			wndComponent.interactable = value;
		}
		else if (property == "progress") {
			wndComponent = wndNode.getComponent(cc.ProgressBar);
			if (!wndComponent) {
				console.error("SetWndProperty(%s,%s,%s) wnd not find cc.ProgressBar", wndPath, property, value);
				return
			}
			wndComponent.progress = value;
		}
		else {
			console.error("SetWndProperty(%s,%s,%s) property error", wndPath, property, value);
			return;
		}
	},
	//获取指定控件的组件对象
	GetWndComponent: function (wndPath, componentType) {

		let wndNode = cc.find(wndPath, this.node);
		if (!wndNode) {
			console.error("GetWndComponent wndPath(%s) error", wndPath);
			return;
		}

		let wndComponent = wndNode.getComponent(componentType);

		if (!wndComponent) {
			console.error("GetWndComponent wndPath(%s) not find Component(%s)", wndPath, componentType.name);
			return;
		}
		return wndComponent;
	},
});
